﻿package level4.mo_ui.mo_radio_button {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	import level4.display.GameDispose;
	import level4.display.helpers.DisplayObjectHelper;
	import level4.interfaces.dispose.IDisposable;
	import level4.mo_ui.mo_events.MoRadioButtonAdapterEvent;
	
	/**
	单选
	1 正常
	2 鼠标移
	3 选中
	4 选中事鼠标移
	*/
	public class MoRadioButtonAdapter extends EventDispatcher implements IDisposable{
		
		private var selected:Boolean = false;
		public var mc:MovieClip;
		public var canSelected:Boolean = true;
		//配置 帧规则
		public var mouseNormal:int = 1;
		public var mouseOver:int = 2;
		public var mouseDown:int = 4;
		public var mouseDownOver:int = 4;
		
		public function MoRadioButtonAdapter(mc:MovieClip) {
			// constructor code
			this.mc = mc;
			init();
		}
		public function init():void{
			DisplayObjectHelper.stopAllChildren(mc);
			mc.addEventListener(MouseEvent.MOUSE_OVER,onOver);
			mc.addEventListener(MouseEvent.MOUSE_OUT,onOut);
			mc.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
			mc.mouseChildren = false;
		}
		public function dispose():void{
			if(mc){
				mc.removeEventListener(MouseEvent.MOUSE_OVER,onOver);
				mc.removeEventListener(MouseEvent.MOUSE_OUT,onOut);
				mc.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);
			}
			mc = null;
		}
		
		private function onOver(e:MouseEvent):void{
			if(selected){
				mc.gotoAndStop(mouseDownOver);
			}else
				mc.gotoAndStop(mouseOver);
		}
		private function onOut(e:MouseEvent):void{
			mc.gotoAndStop(mouseNormal);
			check();
		}
		private function onDown(e:MouseEvent):void{
			if(canSelected){
				selected = !selected;
				check();
				dispatchEvent(new MoRadioButtonAdapterEvent(MoRadioButtonAdapterEvent.CHANGE,this));
			}
		}
		//检查是否选中
		private function check():void{
			if(selected){
				mc.gotoAndStop(mouseDown);
			}else
				mc.gotoAndStop(mouseNormal);
		}
		
		public function set Selected(value:Boolean):void{
			selected = value;
			check();
		}
		public function get Selected():Boolean{
			return selected;
		}
		
	}
	
}
